package com.cleaningcloud.cld.service.impl;

import com.cleaningcloud.cld.api.domain.CldRecruitPlan;
import com.cleaningcloud.cld.domain.dto.CldProjectPostDto;
import com.cleaningcloud.cld.domain.dto.CldRecruitPlanDto;
import com.cleaningcloud.cld.domain.vo.CldProjectPostSetVo;
import com.cleaningcloud.cld.domain.vo.CldRecruitPlanDetailsVo;
import com.cleaningcloud.cld.domain.vo.CldRecruitPlanVo;
import com.cleaningcloud.cld.mapper.CldProjectPostMapper;
import com.cleaningcloud.cld.mapper.CldRecruitPlanDetailsMapper;
import com.cleaningcloud.cld.mapper.CldRecruitPlanMapper;
import com.cleaningcloud.cld.service.ICldRecruitPlanService;
import com.cleaningcloud.common.core.utils.DateUtils;
import com.cleaningcloud.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 招聘计划Service业务层处理
 *
 * @author cleaningcloud
 * @date 2024-10-23
 */
@Service
public class CldRecruitPlanServiceImpl implements ICldRecruitPlanService {
    @Autowired
    private CldRecruitPlanMapper cldRecruitPlanMapper;
    @Autowired
    private CldRecruitPlanDetailsMapper recruitPlanDetailsMapper;
    @Autowired
    private CldProjectPostMapper cldProjectPostMapper;

    public static void main(String[] args) {

        Date date1 = DateUtils.dateTime("yyyy-MM-dd HH:mm:ss", "2025-10-23 10:00:00");
        Date date2 = new Date();
        System.out.println(date1.compareTo(date2));
    }

    /**
     * 查询招聘计划
     *
     * @param recruitPlanId 招聘计划主键
     * @return 招聘计划
     */
    @Override
    public CldRecruitPlanVo selectCldRecruitPlanByRecruitPlanId(Long recruitPlanId) {
        CldRecruitPlanVo cldRecruitPlanVo = cldRecruitPlanMapper.selectCldRecruitPlanByRecruitPlanId(recruitPlanId);
        List<CldRecruitPlanDetailsVo> dutyList = recruitPlanDetailsMapper.selectCldRecruitPlanDetailsByRecruitPlanId(cldRecruitPlanVo.getRecruitPlanId());
        cldRecruitPlanVo.setDutyList(dutyList);
        return cldRecruitPlanVo;
    }

    @Override
    public CldRecruitPlan selectCldRecruitPlanByRecruitPhone(String phone) {
        return cldRecruitPlanMapper.selectCldRecruitPlanByRecruitPhone(phone);
    }

    /**
     * 查询招聘计划列表
     *
     * @param cldRecruitPlan 招聘计划
     * @return 招聘计划
     */
    @Override
    public List<CldRecruitPlanVo> selectCldRecruitPlanList(CldRecruitPlanDto cldRecruitPlan) {
        return cldRecruitPlanMapper.selectCldRecruitPlanList(cldRecruitPlan);
    }

    /**
     * 新增招聘计划
     *
     * @param cldRecruitPlan 招聘计划
     * @return 结果
     */
    @Override
    public int insertCldRecruitPlan(CldRecruitPlan cldRecruitPlan) {
        if (cldRecruitPlan.getRecruitLeadingId() == null) {
            // 默认当前项目主管为负责人
            CldProjectPostDto query = new CldProjectPostDto();
            query.setProjectId(cldRecruitPlan.getProjectId());
            query.setProjectPostName(1L);
            query.setIsMasterDuits(1L);
            query.getParams().put("isNotNullStaffId", "1");
            List<CldProjectPostSetVo> cldProjectPostVos = cldProjectPostMapper.selectCldProjectPostList(query);
            if (cldProjectPostVos.isEmpty()) {
                throw new RuntimeException("当前项目未设置主管！");
            }
            cldRecruitPlan.setRecruitLeadingId(cldProjectPostVos.get(0).getStaffId());
        }
        // 查询该项目在时间节点内是否存在进行中/未开始的招聘计划
        CldRecruitPlanDto recruitQuery = new CldRecruitPlanDto();
        recruitQuery.setProjectId(cldRecruitPlan.getProjectId());
        recruitQuery.getParams().put("recruitPlanStatus", "0,1");
        recruitQuery.getParams().put("isRecruit", DateUtils.dateTime(cldRecruitPlan.getStartTime()));
        recruitQuery.setRecruitLeadingId(cldRecruitPlan.getRecruitLeadingId());
        List<CldRecruitPlanVo> recruitPlanVoList = cldRecruitPlanMapper.selectCldRecruitPlanList(recruitQuery);
        if (!recruitPlanVoList.isEmpty()) {
            throw new RuntimeException("当前项目在时段内已存在未开始/进行中的招聘计划！");
        }
        cldRecruitPlan.setCreateTime(DateUtils.getNowDate());
        // 开始时间小于当前时间 并且结束时间小于当前时间 状态为进行中
        if (cldRecruitPlan.getStartTime().compareTo(new Date()) <= 0 && cldRecruitPlan.getEndTime().compareTo(new Date()) > 0) {
            cldRecruitPlan.setRecruitPlanStatus(1L);
        } else if (cldRecruitPlan.getEndTime().compareTo(new Date()) <= 0) {
            cldRecruitPlan.setRecruitPlanStatus(2L);
        }else{
            cldRecruitPlan.setRecruitPlanStatus(0L);
        }
        cldRecruitPlan.setCreateId(SecurityUtils.getUserId());
        cldRecruitPlan.setCreateName(SecurityUtils.getUsername());
        cldRecruitPlan.setFlag(0L);
        return cldRecruitPlanMapper.insertCldRecruitPlan(cldRecruitPlan);
    }

    /**
     * 修改招聘计划
     *
     * @param cldRecruitPlan 招聘计划
     * @return 结果
     */
    @Override
    public int updateCldRecruitPlan(CldRecruitPlan cldRecruitPlan) {
        return cldRecruitPlanMapper.updateCldRecruitPlan(cldRecruitPlan);
    }

    /**
     * 批量删除招聘计划
     *
     * @param recruitPlanIds 需要删除的招聘计划主键
     * @return 结果
     */
    @Override
    public int deleteCldRecruitPlanByRecruitPlanIds(Long[] recruitPlanIds) {
        return cldRecruitPlanMapper.deleteCldRecruitPlanByRecruitPlanIds(recruitPlanIds);
    }

    /**
     * 删除招聘计划信息
     *
     * @param recruitPlanId 招聘计划主键
     * @return 结果
     */
    @Override
    public int deleteCldRecruitPlanByRecruitPlanId(Long recruitPlanId) {
        return cldRecruitPlanMapper.deleteCldRecruitPlanByRecruitPlanId(recruitPlanId);
    }
}
